package com.frxuexi.shop2307

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.mvvm.BaseViewActivity
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.shop2307.databinding.ActivityMainBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch

@Route(path = "/app/MainActivity")
@AndroidEntryPoint
class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    override fun initData() {
        LiveDataBus.with("nav",String::class.java).observe(this){
            Log.i("lr",it)
            binding.nav.selectedItemId=R.id.cartFragment
        }

    }

    override fun initView() {
        val navController = findNavController(R.id.fragment_main)
        NavigationUI.setupWithNavController(binding.nav,navController)

        binding.nav.getOrCreateBadge(R.id.cartFragment).number=5

        binding.nav.menu.findItem(R.id.cartFragment).setOnMenuItemClickListener {
            binding.nav.removeBadge(R.id.cartFragment)
            return@setOnMenuItemClickListener false
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        val id = intent?.extras?.getInt("id")
        if(id==3) {
            binding.nav.selectedItemId = R.id.cartFragment
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            LiveDataBus.with("pic",String::class.java).postValue(bean?.path!!)
        }
    }


}